<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class FuwuService extends BaseService
{
    /**
     * 服务
     */

    /**
     * 列表
     * @param array $pages【start起始页，limit每页记录数】
     * @param array $pas【type】
     */
    public function getIndex($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbFuwu)->where('del', 1);
        if ($pas['type']) {
            $db = $db->where('type', $pas['type']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('sort', 'asc')
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有记录');
        }
        $types = $this->fuwu_types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '';
            $thumb = file_exists($v['thumb']) ? $v['thumb'] : '';
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'title' => $v['title'],
                'type' => $v['type'],
                'type_name' => $type_name,
                'cnt' => $v['cnt'],
                'price' => toNumber($v['price']),
                'gsprice' => toNumber($v['gsprice']),
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => $thumb,
                'lasttime' => $v['uptime'],
                'sort' => $v['sort'],
                'able' => $v['able'],
                'able_name' => $v['able'] ? '<b style="color: red;">禁用</b>' : '<b style="color: green;">启用</b>',
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 新增
     * @param array $pas
     */
    public function add($pas)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = $this->dbConn($this->dbFuwu)->insertGetId($pas);
        if (!$res) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbFuwu)
            ->where('id', $id)
            ->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function del($id)
    {
        $res = $this->dbConn($this->dbFuwu)
            ->where('del', 1)
            ->where('id', $id)
            ->update(['del' => 2]);
        if (!$res) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 设置
     * @param int $id
     */
    public function setSort($id, $sort)
    {
        $res = $this->dbConn($this->dbFuwu)
            ->where('del', 1)
            ->where('id', $id)
            ->update(['sort' => $sort]);
        if (!$res) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 开关
     * @param int $id
     */
    public function setAble($id)
    {
        $res = $this->dbConn($this->dbFuwu)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, 'no data');
        }
        $able = $res->able ? 0 : 1;
        $res = $this->dbConn($this->dbFuwu)
            ->where('id', $id)
            ->update(['able' => $able]);
        if (!$res) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

//    /**
//     * 录入
//     */
//    public function jiazhengInput()
//    {
//        $dirpath = 'upload/test/';
//        $dir2path = 'upload/jiazheng/';
//        $dirs = scandir($dirpath);
//        if (!isset($dirs[2])) {
//            return $this->toback(-2, 'no day');
//        }
//        $day = $dirs[2];
//        //日期
//        $dayOne = (array) $this->dbConn($this->dbFwDay)
//            ->where('title', $day)
//            ->first();
//        if (!in_array($day, ['.', '..']) && empty($dayOne)) {
//            $this->dbConn($this->dbFwDay)->insertGetId([
//                'title' => $day, 'addtime' => toDate(),
//            ]);
//        }
//        $fpath = $dirpath.$day.'/';
//        $fpath2 = $dir2path.$day.'/';
//        $dirs = scandir($fpath);
//        if (!isset($dirs[2])) {
//            return $this->toback(-2, 'no img');
//        }
//        //图片
//        foreach ($dirs as $v) {
//            $imgOne = (array) $this->dbConn($this->dbFwImg)
//                ->where('del', 1)
//                ->where('day', $v)
//                ->first();
//            if (!in_array($v, ['.', '..']) && empty($imgOne)) {
//                $this->dbConn($this->dbFwImg)->insertGetId([
//                    'day' => $day, 'img' => $fpath2.$v, 'addtime' => toDate(),
//                ]);
//            }
//        }
//        //转移图片目录
//        if (env('APP_ENV') == 'test') {
//            $from = '/www/wwwroot/freecode/api/public/upload/test/'.$day;
//            $to = '/www/wwwroot/freecode/api/public/upload/jiazheng/';
//            exec("mv ".$from." ".$to);
//        }
////        if (env('APP_ENV') == 'local') {
////            $from = 'D:/phpstudy_pro/WWW/jiugefc/api/public/upload/test/'.$day.'/';
////            $to = 'D:/phpstudy_pro/WWW/jiugefc/api/public/upload/jiazheng/'.$day.'/';
////            if(!file_exists($to)){ mkdir($to, 0777); }
////            $fs = scandir($from);
////            foreach ($fs as $v) {
////                rename($from.$v, $to.$v);
////            }
//////            if (rename($from, $to)) {
//////                echo "目录移动成功";
//////            } else {
//////                echo "目录移动失败";
//////            }
////        } else {
////            $from = '/www/wwwroot/freecode/api/public/upload/test/'.$day;
////            $to = '/www/wwwroot/freecode/api/public/upload/jiazheng';
////            exec("mv ".$from." ".$to);
////        }
//        return $this->toback();
//    }
//
//    /**
//     * 日期
//     * @param array $pages【start，limit】
//     */
//    public function jiazhengDay($pages)
//    {
//        $start = ($pages['start'] - 1) * $pages['limit'];
//        $db = $this->dbConn($this->dbFwDay)
//            ->where('del', 1);
//        $total = $db->count();
//        $res = $db->skip($start)->take($pages['limit'])
//            ->orderBy('id', 'desc')
//            ->get();
//        if (!count($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        return $this->toback(0, '', $res, $total);
//    }
//
//    /**
//     * 图片
//     * @param array $pages【start，limit】
//     * @param string $day
//     */
//    public function jiazhengImg($pages, $day)
//    {
//        $start = ($pages['start'] - 1) * $pages['limit'];
//        $db = $this->dbConn($this->dbFwImg)
//            ->where('del', 1)
//            ->where('day', $day);
//        $total = $db->count();
//        $res = $db->skip($start)->take($pages['limit'])
//            ->orderBy('id', 'asc')
//            ->get();
//        if (!count($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        return $this->toback(0, '', $res, $total);
//    }
}